/************************************************************************************/
/*  Program name CAPFAIMP_CAT_BOOK_DEF.sql                                          */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 6 JAN, 2006   NIP Karen         First created                               		*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_CAT_BOOK_DEF';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_CAT_BOOK_DEF;
	

BEGIN
	UPDATE
		FA_CATEGORY_BOOK_DEFAULTS
	SET
		BOOK_TYPE_CODE = CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_CAT_BOOK_DEF
		);

	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_CATEGORY_BOOK_DEFAULTS
			VALUES
			(
			C1.CATEGORY_ID,
			C1.BOOK_TYPE_CODE,
			C1.START_DPIS,
			C1.END_DPIS,
			C1.LIFE_IN_MONTHS,
			C1.DEPRN_METHOD,
			C1.PRORATE_CONVENTION_CODE,
			C1.ITC_ELIGIBLE_FLAG,
			C1.USE_ITC_CEILINGS_FLAG,
			C1.CEILING_NAME,
			C1.BASIC_RATE,
			C1.ADJUSTED_RATE,
			C1.BONUS_RULE,
			C1.DEPRECIATE_FLAG,
			C1.RETIREMENT_PRORATE_CONVENTION,
			C1.PRICE_INDEX_NAME,
			C1.USE_STL_RETIREMENTS_FLAG,
			C1.STL_METHOD_CODE,
			C1.STL_LIFE_IN_MONTHS,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.SUBCOMPONENT_LIFE_RULE,
			C1.MINIMUM_LIFE_IN_MONTHS,
			C1.PRODUCTION_CAPACITY,
			C1.UNIT_OF_MEASURE,
			C1.CAPITAL_GAIN_THRESHOLD,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.USE_DEPRN_LIMITS_FLAG,
			C1.ALLOWED_DEPRN_LIMIT,
			C1.SPECIAL_DEPRN_LIMIT_AMOUNT,
			C1.PERCENT_SALVAGE_VALUE,
			C1.CUA_LIFE_END_DATE,
			C1.CUA_RULE_SET_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/